# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from manhua.items import ManhuaItem


class Manhua2Spider(CrawlSpider):
    name = 'manhua2'
    allowed_domains = ['cnc.dm5.com']
    start_urls = ['http://cnc.dm5.com/manhua-hktw/']

    rules = (
        Rule(LinkExtractor(allow=r'/manhua-hktw(-p\d+)*/'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        item = ManhuaItem()
        temp = response.xpath('//li[@class="red_lj"]/a[1]')
        for i in temp:
            item['name'] = i.xpath('.//@title').extract()
            item['url_part'] = i.xpath('.//@href').extract()
            yield item
